package com.taptech.doufu.services.offline;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import com.taptech.doufu.util.UIUtil;
import android.app.IntentService;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

public class OffLineService extends IntentService {


	public OffLineService() {
		super("");
	}

	public static ExecutorService  downExecutor;
	private OffLineDownLoader downLoader;
	/** 总共多少任务（根据CPU个数决定创建活动线程的个数,这样取的好处就是可以让手机承受得住） */
	private int count = Runtime.getRuntime().availableProcessors() * 3 + 2;
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		downExecutor =  Executors.newFixedThreadPool(count);
	}

	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			if (msg.what==0) {
				UIUtil.toastMessage(OffLineService.this, msg.obj+"");
			}
		};
	};

	@Override
	protected void onHandleIntent(Intent intent) {
		if (intent!=null) {
			OffLineInfoBean bean = (OffLineInfoBean) intent.getSerializableExtra("offline");
			downLoader = new OffLineDownLoader(OffLineService.this, bean);
			downLoader.download(handler);
		}
	}
	
}
